<template>
  <div>
    <page-dialog
      :title="dialogInfo.title"
      v-model:visible="shows"
      :width="dialogInfo.width"
      :bt-loading="dialogInfo.btLoading"
      :bt-list="dialogInfo.btList"
    >
      <div class="dialog-table">
        <NewPageFilter
          v-model:query="filterInfo.query"
          :filter-list="filterInfo.list"
        >
          <template v-slot:filter-button>
            <div class="dialog-table-header">
              <div class="header-left">
                <div class="lable">产品名称：{{ info.commonName }}</div>
                <div class="lable">生产批号：{{ info.batchNumber }}</div>
              </div>
              <div class="header-right">
                <div class="scan-groups">
                  <el-input
                    v-model="filterInfo.keyword"
                    :clearable="true"
                    placeholder="请输入个体码"
                    @keyup.enter.native="search"
                    @input="(e) => (filterInfo.keyword = validSe(e))"
                  ></el-input>
                  <el-button style="margin-left: 10px" @click="search"
                    >搜索</el-button
                  >
                </div>
              </div>
            </div>
          </template>
        </NewPageFilter>
      </div>
      <el-form :model="tableInfo" ref="tableForm" style="width: 100%">
        <dialog-table
          :refresh="tableInfo.refresh"
          :init-curpage="tableInfo.initCurpage"
          v-model:data="tableInfo.data"
          :api="tableInfo.api"
          :check-box="false"
          :tab-index="true"
          :listen-height="false"
          :pager="false"
          :query="filterInfo.query"
          :field-list="tableInfo.fieldList"
          @handleEvent="handleEvent"
        >
        </dialog-table>
      </el-form>
    </page-dialog>
  </div>
</template>

<script setup name="IndividualDetails">
import { getWmsExportStockDetailExtCustomizeListAll } from "@/api/wms.js";
import DialogTable from "@/components/DialogTable/index";
import { getCustomizeSetting } from "@/api/customize";

const emits = defineEmits();
const { proxy } = getCurrentInstance();
const props = defineProps({
  api: {
    type: Function,
    default: getWmsExportStockDetailExtCustomizeListAll,
  },
  showDialog: {
    type: Boolean,
  },
  pageStatus: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {};
    },
  },
  info: {
    type: Object,
    default: function () {
      return {};
    },
  },
});

const selectedScanValue = ref("1");
const scanInput = ref("");
// 过滤相关配置
const filterInfo = ref({
  keyword: "",
  query: {
    curPage: 1,
    pageSize: 99999,
    queryMap: {
      ...props.query,
    },
    customizeUserVO: {},
  },
  list: [{ slot: true, value: "button" }],
});
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  copyData: [],
  api: undefined,
  selections: [],
  fieldList: [],
  handle: {},
});
// 弹窗相关
const dialogInfo = ref({
  title: "个体详情",
  btLoading: false,
  btList: [{ label: "关闭", event: handleClose, show: true }],
});
const selectRow = ref("");

const shows = computed({
  get() {
    return props.showDialog;
  },
  set(newValue) {
    emits("update:showDialog", newValue); // 更新父组件shows
  },
});

let obj = {
  price: {
    label: "单价",
    value: "price",
    formatter: proxy.$fn.TripartiteMethod,
    type: "column",
    componentType: "price",
    permission: !proxy.$permission("wmsAccountExportOrder:price"),
    hidden: true,
    show: false,
  },
  sumAmount: {
    label: "总金额",
    value: "sumAmount",
    formatter: proxy.$fn.TripartiteMethod,
    type: "column",
    componentType: "price",
    permission: !proxy.$permission("wmsAccountExportOrder:price"),
    hidden: true,
    show: false,
  },
  defaultSupplierPrice: {
    label: "默认供应商价格",
    value: "defaultSupplierPrice",
    type: "column",
    componentType: "price",
    permission: !proxy.$permission("wmsAccountExportOrder:price"),
    formatter: proxy.$fn.TripartiteMethod,
    hidden: true,
    show: false,
  },
};
tableInfo.value.fieldList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...obj,
});
getCustomize();

// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random();
}
function search() {
  const search = filterInfo.value.keyword;
  if (search) {
    tableInfo.value.data = tableInfo.value.copyData.filter((data) => {
      return Object.keys(data).some((key) => {
        if (key === "qrCode") {
          return String(data[key]).indexOf(search) > -1;
        } else if (key === "uniqueCode") {
          return String(data[key]).indexOf(search) > -1;
        } else if (key === "factorySubCode") {
          return String(data[key]).indexOf(search) > -1;
        } else if (key === "factoryMainCode") {
          return String(data[key]).indexOf(search) > -1;
        }
      });
    });
  } else {
    tableInfo.value.data = tableInfo.value.copyData;
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false;
  emits("reset");
}
function handleEvent(event, data) {
  switch (event) {
    case "list":
      tableInfo.value.copyData = data;
      break;
  }
}
function getCustomize() {
  let param = {
    tableKey:
      "wmsExportStockDetailSingle-com.slip.spd.entity.vo.wms.WmsExportStockDetailVO",
  };
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data;
      filterInfo.value.query.customizeUserVO = res.data;
      tableInfo.value.fieldList = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail
      );
      tableInfo.value.api = props.api;
      getList();
    }
  });
}
</script>

<style scoped lang="scss">
:deep(.filter-item) {
  width: 100%;
}
.text-style {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.scan-groups {
  display: flex !important;
  align-items: center;
}
</style>
